﻿using System;

using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace IFD2
{
    public partial class BagTag : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //联程行李如何打印？
            if (!IsPostBack)
            {
                if (Session["ACL"].ToString().IndexOf("DA1") < 0)
                {
                    Response.Redirect("unauthorized.aspx");
                    return;
                }
                if (Request.QueryString["bk_id"] == null || Request.QueryString["pax_sn"] == null || Request.QueryString["trip_sn"] == null)
                {
                    lblError.Text = "Illegal page call!";
                    btnPrint.Visible = false;
                }
                else
                {
                    Retreive();
                }
                btnPrint.Attributes.Add("onclick", "javascript:window.print();");
            }
            //for testing
            //btnPrint.Attributes.Add("onclick", "javascript:prnAgent.LocalPrint(\"BaggageTag\",\"test_string\");");
        }

        protected void Retreive()
        {
            SqlCommand cmd = new SqlCommand();
            SqlParameter para;
            DataTable dtTicket = new DataTable();
            string buf;
            StringBuilder sOutput = new StringBuilder();
            cmd.CommandText = "bll_tkt_get_bag_tags_info";
            para = cmd.Parameters.AddWithValue("@bk_id", Request.QueryString["bk_id"].ToString());
            para = cmd.Parameters.AddWithValue("@pax_sn", Request.QueryString["pax_sn"].ToString());
            para = cmd.Parameters.AddWithValue("@trip_sn", Request.QueryString["trip_sn"].ToString());
            para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
            para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
            TinyDAL.Exec(ref cmd, ref dtTicket);
            if (dtTicket.Rows.Count == 0)
            {
                lblError.Text = "Ticket not found!";

                btnPrint.Enabled = false;
                return;
            }
            btnPrint.Visible = true;
            DataRow row = dtTicket.Rows[0];

            buf = "000" + row["pax_sn"].ToString();
            buf = buf.Substring(buf.Length - 3);

            //data for prnAgent Control:
            //sOutput.Append(row["family_name_en"].ToString().Trim() + "|" + row["name_en"].ToString().Trim()+"|");
            //sOutput.Append(row["op_airline"].ToString() + "|" + row["op_flt_no"].ToString() + "|" + row["flt_date"].ToString() + "|");
            //sOutput.Append(row["ori_airport"].ToString().Trim() + "|" + row["des_airport"].ToString().Trim() + "|" + row["cabin"].ToString() + "|");
            //sOutput.Append(row["bag_count"].ToString() + "|" + row["bag_weight"].ToString().Trim() + "|");
            //sOutput.Append(row["bk_id"].ToString() + buf + "|" + row["id_no"].ToString().Trim());
            //btnPrint.Attributes.Add("onclick", "javascript:prnAgent.LocalPrint(\"BaggageTag\",\""+sOutput.ToString()+"\");");


            //data for Windows printer:
            int count;
            count = Convert.ToInt16(row["bag_count"]);
            for (int i = 1; i <= count; i++)
            {
                sOutput.Append("<table border='0'><tr><th colspan='2'>Baggage Tag</th></tr><tr><td>&nbsp</td></tr>");
                sOutput.Append("<tr><td>Name</td><td><b>" + row["family_name"].ToString().Trim() + "/" + row["name"].ToString().Trim());
                sOutput.Append("<br/>" + row["family_name_en"].ToString().Trim() + "/" + row["name_en"].ToString().Trim() + "</b></td></tr>");
                sOutput.Append("<tr><td>Flight</td><td><b>" + row["airline"].ToString() + row["flt_no"].ToString());
                //if (Convert.ToBoolean(row["virtual"]))
                //{
                sOutput.Append("(" + row["op_airline"].ToString() + row["op_flt_no"].ToString() + ")");
                //}
                sOutput.Append("</b></td></tr>");
                sOutput.Append("<tr><td>Date</td><td><b>" + Convert.ToDateTime(row["flt_date"]).ToShortDateString() + "</b></td></tr>");
                sOutput.Append("<tr><td>Trip</td><td><b>" + row["ori_airport"].ToString().Trim() + " - " + row["des_airport"].ToString() + "</b></td></tr>");
                sOutput.Append("<tr><td>Cabin</td><td><b>" + row["cabin"].ToString() + "</b></td></tr>");
                sOutput.Append("<tr><td>Age</td><td><b>" + row["age_type"].ToString() + "</b></td></tr>");
                //sOutput.Append("<tr><td>Gate</td><td><b>" + row["boarding_gate"].ToString() + "</b></td></tr>");
                //sOutput.Append("<tr><td>Boarding Time</td><td><b>" + Convert.ToDateTime(row["boarding_time"]).ToShortTimeString() + "</b></td></tr>");
                sOutput.Append("<tr><td>Seat</td><td><b>" + row["seat_row"].ToString() + row["seat_col"].ToString() + "</b></td></tr>");
                sOutput.Append("<tr><td>Bag. Count</td><td><b>" + i.ToString() + "/" + row["bag_count"].ToString() + "</b></td></tr>");
                sOutput.Append("<tr><td>Total Weight</td><td><b>" + row["bag_weight"].ToString() + "</b></td></tr>");
                sOutput.Append("<tr><td>Ticket</td><td><b>" + row["bk_id"].ToString() + buf + "</b></td></tr>");
                sOutput.Append("<tr><td>ID</td><td><b>" + row["id_no"].ToString() + "</b></td></tr>");
                sOutput.Append("<tr><td colspan='2'><img src='barcode.ashx?source=" + row["id_no"].ToString() + "&height=30&unit_width=1&vertical=F'></td></tr></table>");

                if (i < count)
                {
                    sOutput.Append("<div style='PAGE-BREAK-AFTER: always'></div>");
                }
            }

            litOutput.Text = sOutput.ToString();
            //btnPrint.Attributes.Add("onclick", "javascript:window.print();");

        }
    }
}